package com.tywj.lib.net.retrofit.model

import android.annotation.SuppressLint
import android.os.Build
import android.text.TextUtils
import com.tywj.lib.core.common.util.ResourcesUtil
import java.util.*

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/7
 *     desc   : 网球请求header
 *     version: 1.0.0
 * </pre>
 */
object HttpHeader {
    const val HEAD_KEY_RESPONSE_CODE = "ResponseCode"
    const val HEAD_KEY_RESPONSE_MESSAGE = "ResponseMessage"
    const val HEAD_KEY_ACCEPT = "Accept"
    const val HEAD_KEY_ACCEPT_ENCODING = "Accept-Encoding"
    const val HEAD_VALUE_ACCEPT_ENCODING = "gzip, deflate"
    const val HEAD_KEY_ACCEPT_LANGUAGE = "Accept-Language"
    const val HEAD_KEY_CONTENT_TYPE = "Content-Type"
    const val HEAD_KEY_CONTENT_LENGTH = "Content-Length"
    const val HEAD_KEY_CONTENT_ENCODING = "Content-Encoding"
    const val HEAD_KEY_CONTENT_DISPOSITION = "Content-Disposition"
    const val HEAD_KEY_CONTENT_RANGE = "Content-Range"
    const val HEAD_KEY_RANGE = "Range"
    const val HEAD_KEY_CACHE_CONTROL = "Cache-Control"
    const val HEAD_KEY_CONNECTION = "Connection"
    const val HEAD_VALUE_CONNECTION_KEEP_ALIVE = "keep-alive"
    const val HEAD_VALUE_CONNECTION_CLOSE = "close"
    const val HEAD_KEY_DATE = "Date"
    const val HEAD_KEY_EXPIRES = "Expires"
    const val HEAD_KEY_E_TAG = "ETag"
    const val HEAD_KEY_PRAGMA = "Pragma"
    const val HEAD_KEY_IF_MODIFIED_SINCE = "If-Modified-Since"
    const val HEAD_KEY_IF_NONE_MATCH = "If-None-Match"
    const val HEAD_KEY_LAST_MODIFIED = "Last-Modified"
    const val HEAD_KEY_LOCATION = "Location"
    const val HEAD_KEY_USER_AGENT = "User-Agent"
    const val HEAD_KEY_COOKIE = "Cookie"
    const val HEAD_KEY_COOKIE2 = "Cookie2"
    const val HEAD_KEY_SET_COOKIE = "Set-Cookie"
    const val HEAD_KEY_SET_COOKIE2 = "Set-Cookie2"

    /**
     * 设置网络请求超时的key，这几个key是为了在构建请求的时候设置和默认请求不一样的超时时间，
     * 只会存在与构建请求的过程中，在实际的网络请求中并不存在这几个key
     */
    const val HEAD_SINGLE_REQUEST_CONNECT_TIMEOUT = "singleRequestConnectTimeOut"
    const val HEAD_SINGLE_REQUEST_READ_TIMEOUT = "singleRequestReadTimeOut"
    const val HEAD_SINGLE_REQUEST_WRITE_TIMEOUT = "singleRequestWriteTimeOut"

    /**
     * User-Agent: Mozilla/5.0 (Linux; U; Android 5.0.2; zh-cn; Redmi Note 3 Build/LRX22G)
     * AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36
     */
    @SuppressLint("PrivateApi")
    fun getUserAgent(): String {
        val sb = StringBuilder()

        // add version
        val version = Build.VERSION.RELEASE
        if (version.isNotEmpty()) {
            sb.append(version)
        } else {
            // default to "1.0"
            sb.append("1.0")
        }
        sb.append("; ")

        // add language and country
        val locale = Locale.getDefault()
        val language = locale.language
        if (language != null) {
            sb.append(language.toLowerCase(locale))
            val country = locale.country
            if (!TextUtils.isEmpty(country)) {
                sb.append("-")
                sb.append(country.toLowerCase(locale))
            }
        } else {
            // default to "en"
            sb.append("en")
        }

        // add the model for the release build
        if ("REL" == Build.VERSION.CODENAME) {
            val model = Build.MODEL
            if (model.isNotEmpty()) {
                sb.append("; ")
                sb.append(model)
            }
        }
        val id = Build.ID
        if (id.isNotEmpty()) {
            sb.append(" Build/")
            sb.append(id)
        }

        var webUserAgent = ""
        try {
            val sysResCls = Class.forName("com.android.internal.R\$string")
            val webUserAgentField = sysResCls.getDeclaredField("web_user_agent")
            val resId = webUserAgentField.get(null) as Int
            webUserAgent = ResourcesUtil.getString(resId)
        } catch (e: Exception) {
            // We have nothing to do
        }

        return if (TextUtils.isEmpty(webUserAgent)) {
            webUserAgent = "Retrofit/PandaCustomer"
            "$webUserAgent Android$sb"
        } else {
            String.format(webUserAgent, sb, "Mobile ")
        }
    }
}